﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

using YTCMS.Core;
using YTCMS.Services.Services;
using YTCMS.Domain.Entities;
using System.Data;

namespace Admin.Common
{
    public class YTDDL
    {
        private static IUnityContainer GetContainer()
        {
            IUnityContainer container = new UnityContainer();
            string file = HttpContext.Current.Server.MapPath("~/config/Unity.config");
            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = file };
            var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var configuration = (UnityConfigurationSection)config.GetSection(UnityConfigurationSection.SectionName);
            configuration.Configure(container, "defaultContainer");
            return container;
        }
        public static IEnumerable<SelectListItem> UserLevel(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.UserLevel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.UserLevel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }

        public static IEnumerable<SelectListItem> CommonState(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.CommonState>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.CommonState>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> HotelLevel(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.Hotellevel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.Hotellevel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> RoomType(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.RoomType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.RoomType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> HotelType(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.HotelType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.HotelType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> PayType(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.PayType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.PayType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }

        public static IEnumerable<SelectListItem> Version(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.Version>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.Version>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }

        public static IEnumerable<SelectListItem> OrderState(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.OrderState>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.OrderState>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> OrderType(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.OrderType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.OrderType>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> Category(Expression<Func<category, bool>> func, bool? isdefault = false)
        {
            IUnityContainer container = GetContainer();

            categoryService project = container.Resolve<categoryService>() as categoryService;
            var v = project.GetMany(func).ToList();
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(v.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() }));
            else
                return v.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() });
        }


        public static IEnumerable<SelectListItem> Module(Expression<Func<module, bool>> func, bool? isdefault = false)
        {
            IUnityContainer container = GetContainer();

            moduleService project = container.Resolve<moduleService>() as moduleService;
            var v = project.GetMany(func).ToList();
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(v.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() }));
            else
                return v.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() });
        }

        public static IEnumerable<SelectListItem> Cmscolumn(Expression<Func<cmscolumn, bool>> func, bool? isdefault = false)
        {
            IUnityContainer container = GetContainer();

            cmscolumnService project = container.Resolve<cmscolumnService>() as cmscolumnService;
            var v = project.GetMany(func).ToList();
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(v.Select(s => new SelectListItem() { Text = s.Title, Value = s.Id.ToString() }));
            else
                return v.Select(s => new SelectListItem() { Text = s.Title, Value = s.Id.ToString() });
        }

        public static IEnumerable<SelectListItem> ProductTags(bool? isdefault = false, string value = null)
        {
            var tagsurl = HttpContext.Current.Server.MapPath(ConfigHelper.GetValue("Tags"));
            var tb = XmlHelper.GetDataSet(tagsurl, XmlHelper.XmlType.String);
            var list = new List<SelectListItem>();
            foreach (DataRow item in tb.Tables[0].Rows)
            {
                SelectListItem si = new SelectListItem();
                var val = item["value"].ToString();
                var text = item["tag_Text"].ToString();
                si.Value = val;
                si.Text = text;
                list.Add(si);
            }
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(list);
            else
                return list;
        }

        public static IEnumerable<SelectListItem> Hotel(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.Hotel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.Hotel>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
        public static IEnumerable<SelectListItem> Vehicle(bool? isdefault = false, string value = null)
        {
            if (isdefault.Value)
                return new List<SelectListItem>() { new SelectListItem() { Text = "请选择", Value = "", Selected = true } }.Union(EnumExtend.ToListItem<YTCMS.Core.Vehicle>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value }));
            else
                return EnumExtend.ToListItem<YTCMS.Core.Vehicle>().Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Value == value });
        }
    }
}